// pages/carts/carts.js
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        carts: null,
        checkedAll: false, //全选的选中状态
        total: 0, //总价
        sendPrice: 0,//配送费
        show: false,//控制点去核算没有选商品时，提示框的显示和隐藏
        num: 0,//去核算商品的数量
    },

    // 获取存在本地的数据
    getCarts() {
        let carts = wx.getStorageSync('cartArr');
        this.setData({
            carts
        })
    },
    // 去首页
    goIndex() {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },

    // 每一个商品被选中时的状态
    onChange(event) {
        this.setData({
            checked: event.detail,
        });
    },
    // 点击全选按钮
    selectAll() {
        this.setData({
            checkedAll: !this.data.checkedAll
        })
        let list = this.data.carts;
        for (let i = 0; i < list.length; i++) {
            list[i].checked = this.data.checkedAll;
        }

        let allSelectShop = list.filter(item => {
            return item.checked == true
        })
        let total = allSelectShop.reduce((total, item) => {
            return total + item.price.split("￥")[1] * item.num
        }, 0)
        let num = 0;
        for (var i = 0; i < allSelectShop.length; i++) {
            if (allSelectShop[i].checked) {
                num++
            }
        }
        wx.setStorageSync('cartArr', list)
        this.setData({
            carts: list,
            total: total.toFixed(2),
            num
        })
    },
    // 点击一个复选框
    select(e) {
        // 单选控制全选
        let index = e.currentTarget.dataset.key;
        let list = this.data.carts;
        list[index].checked = !list[index].checked;

        let num = 0;
        for (var i = 0; i < list.length; i++) {
            if (list[i].checked) {
                num++
            }
        }
        if (num == list.length) {
            this.setData({
                checkedAll: true
            })
        } else {
            this.setData({
                checkedAll: false
            })
        }

        // 获取所有被选中的商品
        let allSelectShop = list.filter(item => {
            return item.checked == true
        })

        let total = allSelectShop.reduce((total, item) => {
            return total + item.price.split("￥")[1] * item.num
        }, 0)
        this.setData({
            total: total.toFixed(2),
            num: allSelectShop.length
        })
    },
    //点击加减按钮 
    countfn(e) {
        let num = e.detail;
        let index = e.currentTarget.dataset.key;
        let list = this.data.carts;
        list[index].num = num;

        let allSelectShop = list.filter(item => {
            return item.checked == true
        })
        let total = allSelectShop.reduce((total, item) => {
            return total + item.price.split("￥")[1] * item.num
        }, 0)
        this.setData({
            carts: list,
            total: total.toFixed(2)
        })
    },

    // 点击删除按钮
    deleteShop(e) {
        Dialog.confirm({
            title: '温馨提示',
            message: '你确定要删除订单吗',
        })
            .then(() => {
                // 确定

                // 获取全选的选中状态
                if (this.data.checkedAll == false) {
                    let key = e.currentTarget.dataset.key;
                    let cartArr = wx.getStorageSync('cartArr');
                    cartArr.splice(key, 1);
                    wx.setStorageSync('cartArr', cartArr)

                    let list = wx.getStorageSync('cartArr');
                    for (let i = 0; i < list.length; i++) {
                        list[i].checked = false;
                    }
                    this.setData({
                        carts: list
                    })
                } else {
                    this.setData({
                        checkedAll: true
                    })
                    let key = e.currentTarget.dataset.key;
                    let cartArr = wx.getStorageSync('cartArr');
                    cartArr.splice(key, 1);

                    // 获取所有被选中的商品
                    let allSelectShop = cartArr.filter(item => {
                        return item.checked == true
                    })
                    let total = allSelectShop.reduce((total, item) => {
                        return total + item.price.split("￥")[1] * item.num
                    }, 0)

                    this.setData({
                        carts: cartArr,
                        total: total.toFixed(2),
                        num: allSelectShop.length
                    })
                    wx.setStorageSync('cartArr', cartArr)
                }
            })
            .catch(() => {
                // 取消
                // on cancel
            });
    },

    // 切换tabBar页面的时候会触发这个事件
    onTabItemTap() {
        this.setData({
            carts: wx.getStorageSync('cartArr')
        })
    },

    // 点去核算跳转到订单页面
    calculate() {
        //获取被选中的商品
        let list = this.data.carts;
        let allSelectShop = list.filter(item => {
            return item.checked == true
        })
        let sendPrice = allSelectShop.reduce((sendPrice, item) => {
            return sendPrice + item.sendprice
        }, 0)
        this.setData({
            sendPrice
        })

        if (allSelectShop.length > 0) {
            wx.navigateTo({
                url: "/pages/order/order?allSelectShop=" + JSON.stringify(allSelectShop) + "&total=" + this.data.total + "&sendPrice=" + this.data.sendPrice
            })
        } else {
            this.setData({ show: true })
            setTimeout(() => {
                this.setData({
                    show: false
                })
            }, 900)
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCarts();
        let list = this.data.carts;
        for (let i = 0; i < list.length; i++) {
            list[i].checked = false;
            if (list[i].type == "index") {
                list[i].className = "image"
            } else {
                list[i].className = "";
            }
        }
        this.setData({
            carts: list,
            total: this.data.total.toFixed(2),
        })
        wx.setStorageSync('cartArr', list)

        if (options.type == "pay") {
            console.log(1)
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})